#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class NoteRangeProvider : CY.GFive.Core.Providers.DALProvider.INoteRangeProvider
	{
		#region Sql
		private static readonly string SqlSelectNoteRange = "SELECT [Id], [NoteKey],[ModuleKey],[ReciverType],[ReciverKey],[NoteState] FROM [NoteRange] WHERE  [Id]=@Id";

		private static readonly string SqlInsertNoteRange = "INSERT INTO [NoteRange]([NoteKey],[ModuleKey],[ReciverType],[ReciverKey],[NoteState]) values(@NoteKey,@ModuleKey,@ReciverType,@ReciverKey,@NoteState);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateNoteRange = "UPDATE [NoteRange] SET [NoteKey] = @NoteKey,[ModuleKey] = @ModuleKey,[ReciverType] = @ReciverType,[ReciverKey] = @ReciverKey,[NoteState] = @NoteState WHERE  [Id]=@Id";

		private static readonly string SqlDeleteNoteRange = "DELETE [NoteRange] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllNoteRange = "SELECT [Id], [NoteKey],[ModuleKey],[ReciverType],[ReciverKey],[NoteState] FROM [NoteRange]";
		#endregion

		#region INoteRangeProvider Members
		public Core.Business.NoteRange Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectNoteRange);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.NoteRange noteRange = new Core.Business.NoteRange();

				if (!reader.IsDBNull(0)) noteRange.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) noteRange.NoteKey = reader.GetInt32(1);
				if (!reader.IsDBNull(2)) noteRange.ModuleKey = reader.GetString(2);
				if (!reader.IsDBNull(3)) noteRange.ReciverType = reader.GetInt32(3);
				if (!reader.IsDBNull(4)) noteRange.ReciverKey = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) noteRange.NoteState = reader.GetInt32(5);

				return noteRange;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.NoteRange noteRange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@NoteKey", SqlDbType.Int, noteRange.NoteKey);
			sql.AddParameter("@ModuleKey", SqlDbType.NVarChar, noteRange.ModuleKey);
			sql.AddParameter("@ReciverType", SqlDbType.Int, noteRange.ReciverType);
			sql.AddParameter("@ReciverKey", SqlDbType.Int, noteRange.ReciverKey);
			sql.AddParameter("@NoteState", SqlDbType.Int, noteRange.NoteState);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertNoteRange);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) noteRange.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.NoteRange noteRange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@NoteKey", SqlDbType.Int, noteRange.NoteKey);
			sql.AddParameter("@ModuleKey", SqlDbType.NVarChar, noteRange.ModuleKey);
			sql.AddParameter("@ReciverType", SqlDbType.Int, noteRange.ReciverType);
			sql.AddParameter("@ReciverKey", SqlDbType.Int, noteRange.ReciverKey);
			sql.AddParameter("@NoteState", SqlDbType.Int, noteRange.NoteState);
			sql.AddParameter("@Id", SqlDbType.Int, noteRange.Id);
			sql.ExecuteSql(SqlUpdateNoteRange);
		}

		public void Delete(Core.Business.NoteRange noteRange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, noteRange.Id);
			sql.ExecuteSql(SqlDeleteNoteRange);
		}

		public IList<Core.Business.NoteRange> GetAllNoteRange()
		{
			IList<Core.Business.NoteRange> noteRangelist = new List<Core.Business.NoteRange>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllNoteRange);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.NoteRange noteRange = new Core.Business.NoteRange();

					if (!reader.IsDBNull(0)) noteRange.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) noteRange.NoteKey = reader.GetInt32(1);
					if (!reader.IsDBNull(2)) noteRange.ModuleKey = reader.GetString(2);
					if (!reader.IsDBNull(3)) noteRange.ReciverType = reader.GetInt32(3);
					if (!reader.IsDBNull(4)) noteRange.ReciverKey = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) noteRange.NoteState = reader.GetInt32(5);

					noteRange.MarkOld();
					noteRangelist.Add(noteRange);
				}
				reader.Close();
			}
			return noteRangelist;
		}
		#endregion
	}
}
